﻿var sFkGUID;

//上传文档
function upFile()
{    
    hideMenu();
    var mode = document.getElementById("__mode").value;
    var sTitle = "上传文档";
    var sWidth = "500px";
    //BUG 44934 对话框出现状态栏，显示不完全 sunfx 2009年4月28日14:29:19
    var sHeight = "250px"; 	
    //BUG 44934 END
    var sFile = "/_controls/upfile/UpFile_Main_Edit.aspx";    
    
    var sParamList = escape("FkGUID"); 
    sParamList += "=" + escape(sFkGUID); 
    sParamList += "&" + escape("sTitle"); 
    sParamList += "=" + escape(sTitle); 	
    sParamList += "&" + escape("DocType"); 
    sParamList += "=" + escape("工作流"); 
    sParamList += "&" + escape("PageState"); 
    sParamList += "=" + escape("EDIT"); 
    
    var ReturnValue = __OpenModalWinUpFile(sTitle,sFile,sParamList,sWidth,sHeight);
    if(ReturnValue=="ok"){
        getDocHtml();
    }
}

//得到附件列表
function getDocHtml(){
    //myXmlHttp.callByAsync("/MyWorkflow/WF_XmlHTTP.aspx?ywtype=GetDocHTML&ywtxt=&ywonlyflag=" + sFkGUID + "&rdnum=" + Math.random(),"",bandDocHtml);
    var strHtml = GetDataFromXMLHTTP("/MyWorkflow/WF_XmlHTTP.aspx","GetDocHTML",sFkGUID,"","");
	if(sDocType=="0"||sDocType=="2"){
   		document.getElementById("divAttachMent").innerHTML = strHtml;
   		return;
    }
   	if(sDocType=="1"||sDocType=="3")	
   		document.getElementById("_divAttachMent").innerHTML = strHtml;
}

function bandDocHtml(strHtml){
	if($id("divAttachMent"))
   		$id("divAttachMent").innerHTML = strHtml;
   	if($id("_divAttachMent"))	
   		$id("_divAttachMent").innerHTML = strHtml;
}

//功能：签入

function FileCheckin(oid,bAlert)
{
    if(bAlert==undefined) bAlert=true;
	
    var sResult = GetDataFromXMLHTTP("/MyWorkflow/WF_XmlHTTP.aspx","AttachMentDocCheckIn","",oid,"");
    if(sResult=="-1") return false;
	
    var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
	
    xmlDom.loadXML(sResult);

    if(xmlDom.documentElement.attributes.getNamedItem("result").value == "true" )
    {
	    return true;
    }
    else
    {
	    if(bAlert) alert("签入失败，文档可能已被签入！");
	    return false;
    }
}

//功能：签出

function FileCheckout(oid)
{
    var sResult = GetDataFromXMLHTTP("/MyWorkflow/WF_XmlHTTP.aspx","AttachMentDocCheckOut",document.getElementById("__oid").value,oid,"");

    if(sResult=="-1") return false;
  
    //签出前记录Word文档大小日志信息
    SaveWordLog(oid, "签出审批附件文档", "FJ")
    
    var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
	
    xmlDom.loadXML(sResult);
	
    if(xmlDom.documentElement.attributes.getNamedItem("result").value == "true" )
    {
	    return 1;
    }
    else if(xmlDom.documentElement.attributes.getNamedItem("result").value == "already") 
    {
	    return 0;
    }
    else
    {
	    alert(xmlDom.documentElement.attributes.getNamedItem("errmessage").value);
	    return -1;
    }
}

function oprEdit(o)
{
	var sUrl = "WF_AttachMent_Word.aspx?mode="
	sUrl += 2;
	//CanDocComment
	sUrl += "&canDocComment=" + appForm.CanDocComment.value;
	//CanDocModify
	sUrl += "&canDocModify=" + appForm.CanDocModify.value;
	//CanDocFinalize
	sUrl += "&canDocFinalize=" + (appForm.HandleType.value == "[协商]"?"0":appForm.CanDocFinalize.value);
	
	sUrl += "&oid=" + o.oid;
	var extendname = getDocExtendName(o.filename);
	
    if(extendname=="DOC"||extendname=="DOCX")
        sUrl += "&doctype=1"; 
    else
        sUrl += "&doctype=3";
	
	sUrl += "&docname=" + escape(o.docname);
	sUrl += "&NodeGUID=" + escape(document.getElementById("__oid").value);
	openMyFullWin(sUrl);
}

//查看Word文档
function oprView(o){
    var sUrl = "WF_AttachMent_Word.aspx?mode="
	sUrl += 3;
		sUrl += "&oid=" + o.oid;
	var extendname = getDocExtendName(o.filename);
	
    if(extendname=="DOC"||extendname=="DOCX")
        sUrl += "&doctype=1"; 
    else
        sUrl += "&doctype=3";
	
	sUrl += "&docname=" + escape(o.docname);
	openMyFullWin(sUrl);
}


//功能：判断签出状态,生成菜单项
function checkMenuItem(obj)
{
    var extendname = getDocExtendName(obj.filename);
    if("DOC,DOCX".indexOf(extendname)<0){
        if (appForm.CanDocModify.value == "1" && obj.createby == $id("txtUserGUID").value) return "1000111";
        return "1000001";
    }
    //判断签出    
    var sResult = GetDataFromXMLHTTP("/MyWorkflow/WF_XmlHTTP.aspx","IsAttachMentCheckOut","",obj.oid,"");
    if(sResult=="-1") return "1000001";
	
    var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
	
    xmlDom.loadXML(sResult);

    if(xmlDom.documentElement.attributes.getNamedItem("result").value == "true" )
    {
        if (xmlDom.documentElement.attributes.getNamedItem("lockedby").value==$id("txtUserGUID").value) return "1011101";
        return "1000001";
	}
    else
    {
        if ($id("HandleType")!=null && $id("HandleType").value=="[指派]")
            return "1000001";
        else
        {            
            if (appForm.CanDocModify.value == "1" && obj.createby == $id("txtUserGUID").value)
	            return "1100111";
	        else if(appForm.CanDocComment.value == "1" ||  appForm.CanDocFinalize.value == "1" || appForm.CanDocModify.value == "1")
	            return "1100001";
	        else return "1000001";
	    }
    }
}

function menuclick(o,str)
{
    var reV;
    switch(str){
        case "select":
            var extendname = getDocExtendName(o.filename);
            if("DOC,DOCX".indexOf(extendname)<0){
                __DownUpFile_FZ(o.docname,o.filename,"_blank");    
            }else{
                oprView(o);
            }
           break;
        case "checkOut":
            FileCheckout(o.oid);
            getDocHtml();
            break;
        case "edit":
            oprEdit(o);
            break;
        case "checkIn":
            FileCheckin(o.oid,true);
            getDocHtml();
            break;
        case "rename":
            hideMenu();
            reV = __OpenModalWinUpFile("文档更名","/MyWorkflow/DocumentRename.aspx","oid="+escape(o.oid)+"&docname="+escape(o.docname),"400px","171px");// xp sp2 ie6 调整高度 sunfx 2009年4月30日10:09:12
            if(reV != "" && reV != undefined ){
                 getDocHtml();
            }
            break;
        case "del":
            if(confirm("附件将被删除，是否继续？")){
                reV = GetDataFromXMLHTTP("/MyWorkflow/WF_XmlHTTP.aspx","DeleteUpFile",o.oid,"","");
                if(reV){
                    getDocHtml();
                }
            }
            break;
        case "attribute":
            showAttribute(o);            
            break;
    }
}

function showAttribute(obj){
    var divA = document.getElementById("divAttribute");
    var rowObjs = divA.getElementsByTagName("td");
    rowObjs[1].innerText = obj.modifieduser;
    rowObjs[3].innerText = obj.modifiedtime;
    rowObjs[5].innerText = obj.createname;
    rowObjs[7].innerText = obj.createon;
    
    divA.style.left = document.getElementById("menu").offsetLeft + document.getElementById("menu").offsetWidth;
    
    var bottomedge=document.body.clientHeight - document.getElementById("menu").offsetTop - document.getElementById("menu").clientHeight;
    if (bottomedge < 50)
        divA.style.top = document.getElementById("menu").offsetTop + document.getElementById("menu").clientHeight - 15 - 50;
    else
        divA.style.top = document.getElementById("menu").offsetTop + document.getElementById("menu").clientHeight - 15;

    divA.style.display="";
    return false;
}

function showMenu(obj){
    createMenuItem(obj);//生成菜单项

    $id("menu").style.display = "block";
    var rightedge=document.body.clientWidth-event.clientX;
    var bottomedge=document.body.clientHeight-event.clientY;

    if (rightedge<document.getElementById("menu").offsetWidth)
        document.getElementById("menu").style.left=document.body.scrollLeft+event.clientX-document.getElementById("menu").offsetWidth;
    else
        document.getElementById("menu").style.left=document.body.scrollLeft+event.clientX;

    if (bottomedge<document.getElementById("menu").offsetHeight)
        document.getElementById("menu").style.top=document.body.scrollTop+event.clientY-document.getElementById("menu").offsetHeight;
    else
        document.getElementById("menu").style.top=document.body.scrollTop+event.clientY;


    return false;
}

function hideMenu(){
    document.getElementById("menu").style.display="none";
}

function createMenuItem(obj){
    if(document.getElementById("aUpFile").style.display == "none"){
        menuItem = "1000001";
    }else{
        var mode = document.getElementById("__mode").value;
        switch(mode){
            case "1":
                menuItem = "1000111";
                break;
            case "2":            
                if(appForm.CanDocModify)                    
                    menuItem = checkMenuItem(obj);  //审批
                else
                    menuItem = "1000111";           //发起
                break;
            case "3":
                menuItem = "1000001";
                break;
        }
    }
    
    var rowObjs = document.getElementById("menu").all.tags("div");
    var rowCount=rowObjs.length;
           
    for(var i=0;i<rowObjs.length;i++)
    {
        var hide = menuItem.charAt(i)!='1';
        rowObjs[i].style.color = hide?"#C5C5C5":"black";
        
        if(!hide){
            rowObjs[i].onmouseover=function(){
                this.style.backgroundColor="highlight";
            }
                
            rowObjs[i].onmouseout=function(){
                this.style.backgroundColor="";
            }
            
            if(rowObjs[i].action != "attribute"){
                rowObjs[i].onclick = function()
                {
                    menuclick(obj,this.action);
                }
            }else{
                rowObjs[i].onmouseover = function()
                {
                    menuclick(obj,this.action);
                    this.style.backgroundColor="highlight";
                }
                rowObjs[i].onmouseout=function(){
                    this.style.backgroundColor="";
                    document.getElementById("divAttribute").style.display = "none";
                }
            }
        }else{
            rowObjs[i].onclick = null;
            rowObjs[i].onmouseover = null;
            rowObjs[i].onmouseout = null;
        }
    }
    document.getElementById("menu").onclick = hideMenu;
    document.body.onclick = hideMenu;
}